<template>
  <div>
    <div>
      <input v-model="inputValue" @focusin="handleFocusIn" @focusout="handleFocusOut" />
      <button @click="handleClick">提交</button>
    </div>
    <todo-item v-show="showList" v-for="(item,index) of list" :key="index" :content="item" :index="index" @delete="handleDelete" />
  </div>
</template>

<script>
  import TodoItem from './components/TodoItem'
  export default {
    name: 'App',
    data() {
      return {
        list: [],
        inputValue: '',
        showList: false
      }
    },
    components: {
      'todo-item': TodoItem
    },
    methods: {
      handleClick() {
        var length = this.inputValue.replace(/(^\s*)|(\s*$)/g, "");
        if (length <= 0) {
          this.inputValue = ''
          return;
        }else{
          this.list.push(this.inputValue)
          this.inputValue = ''
        }

      },
      handleDelete(index) {
        this.list.splice(index, 1)
      },
      handleFocusIn() {
        this.showList = true;
      },
      handleFocusOut() {
        this.showList = false;
      }
    },
    watch: {
      // inputValue() {
      //   var str = this.inputValue;
      //   this.inputValue = str.replace(/(^\s*)|(\s*$)/g, ""); // str.replace(/\s+/g, "");
      // }
    }
  }
</script>

<style>

</style>
